package cn.biplam.front.template.control.rfFlowTable;

import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.common.wrapper.Result;
import cn.biplam.common.wrapper.UserInfo;
import cn.biplam.common.wrapper.treeFlat.flat.callback.CompareCallback;
import cn.biplam.front.component.data.common.ButtonComp;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.template.control.rfFlowTable.callback.action.ActionCallback;
import cn.biplam.front.template.control.rfFlowTable.callback.content.ContentCallback;
import cn.biplam.front.template.control.table.Table;
import cn.biplam.front.template.control.table.TableScript;
import cn.biplam.front.template.control.table.cell.ThCell;

/**
 * 表格列创建器<BR>
 * 单独为ReFlowTableImp类服务<BR>
 * 提供创建表格单元格的服务<BR>
 * @author XiaoRuxing Nov 6, 200911:59:06 AM
 * @version 1.0
 */
public class RfFlowTableColCreater {

	/**
	 *  创建表格标题列
	 * @param colTitle 标题内容
	 * @param colWidth 列宽度
	 * @param table    表格容器
	 * @param colNumber 列下标
	 */
	protected static void createTitleCell(String colTitle, int colWidth,
			Table table,int colNumber) throws ErrorMessage{
		HtmlWindow content = new ThCell(colTitle).get();
		table.put(content, 0, colNumber,Table.Table_Cell_Type.TH);
		if(colWidth>0){
			table.setColWidth(String.valueOf(colWidth), 0, colNumber);
		}
	}

	/**
	 *  创建操作按钮
	 * @param sHomeUrl
	 * @param mUser
	 * @param buttonCaption   按钮标题
	 * @param actionCallback  动作回调器
	 * @param compareCallback 比较回调器
	 * @param contentCallback 按钮标题回调器
	 * @param rowIndex
	 * @param recordId
	 * @param result
	 * @return 操作列
	 * @throws ErrorMessage
	 */
	protected static HtmlWindow createOperatorCol(
			String sHomeUrl,
			UserInfo mUser,
			String buttonCaption,
			ActionCallback actionCallback,
			CompareCallback compareCallback,
			ContentCallback contentCallback,
			String rowIndex,
			String recordId,
			Result result) throws ErrorMessage{

		HtmlWindow curBt=null;
		boolean isValid = true;//按钮是有可点击,默认为ture
		//根据比较回调器判断按钮是否可点击
		if(compareCallback!=null){
			isValid = compareCallback.get(result);
		}
		//根据标题回调器获取按钮标题
		if(contentCallback!=null){
			buttonCaption=contentCallback.getContent(result);
		}
		//创建按钮
		curBt = ButtonComp.get(mUser, buttonCaption, isValid);
		//绑定按钮事件
		if(actionCallback!=null){
			curBt.bind(Window_Event.OnClick, 
					getActionScript(actionCallback, rowIndex, recordId, result)
			);
		}
		return curBt;
	}

	/**
	 *  根据脚本回调器生成脚本
	 */
	private static String getActionScript(ActionCallback actionCallback,
			String rowIndex,
			String recordId,
			Result result
			) throws ErrorMessage{
		if(actionCallback==null)return null;
		String script = null;
		if(!actionCallback.isCustomerModel()){
			actionCallback.process(result);//获取自定义提交动作
			script = TableScript.getBindScript(
					actionCallback,//绑定动作回调器
					rowIndex,   //提交容器为当前行
					recordId);  //附加参数
		}else{
			script = actionCallback.getCustomerScript(result, recordId);
		}
		return script;
	}
	
	/**
	 * 创建标题行
	 */
	protected static void createTableTitle(RfFlowTablePropertys property,
			Table table) throws ErrorMessage{
		String colTitle;//标题名称
		int colWidth;   //行宽
		String colName; //列名称
		int colsIndex;
		for(colsIndex=0;colsIndex<property.getColCount(); colsIndex++){
			colName = property.getFieldName(colsIndex);
			colTitle = property.getFieldCaption(colName);//标题
			colWidth = property.getColWidth(colName);//宽度
			RfFlowTableColCreater.createTitleCell(colTitle, colWidth, table, colsIndex);
		}
		//创建按钮列标题
		if(property.getBtCount()>0){
			RfFlowTableColCreater.createTitleCell(property.getOperatorColTitle(), 
					-1, table, colsIndex);
		}
	}
}
